import java.util.Arrays;
import java.util.stream.Collectors;

public class Leetcode2042 {

    public static void main(String[] args) {
        System.out.println(areNumbersAscending("1 box has 3 blue 4 red 6 green and 12 yellow marbles"));
        System.out.println(areNumbersAscending("hello world 5 x 5"));
        System.out.println(areNumbersAscending("sunset is at 7 51 pm overnight lows will be in the low 50 and 60 s"));
        System.out.println(areNumbersAscending("4 5 11 26"));
    }

    public static boolean areNumbersAscending1(String s) {
        char[] chars = s.toCharArray();

        int parent = Integer.MIN_VALUE;
        for (int i = 0; i < chars.length; i++) {
            if (chars[i] < '0' || chars[i] > '9') {
                continue;
            }


            int nextIndex = i;
            int next = 0;
            while (nextIndex < chars.length && chars[nextIndex] != ' ') {
                next = next * 10 + (chars[nextIndex++] - '0');
            }

            if (parent >= next) {
                return false;
            }

            parent = next;

            i = nextIndex;
        }

        return true;
    }

    public static boolean areNumbersAscending(String s) {
        return Arrays.stream(s.split(" "))
                     .filter(s1 -> Character.isDigit(s1.charAt(0)))
                     .map(Integer::parseInt)
                     .sorted()
                     .map(String::valueOf)
                     .collect(Collectors.joining(" "))
                     .equals(Arrays.stream(s.split(" "))
                                   .filter(s1 -> Character.isDigit(s1.charAt(0)))
                                   .collect(Collectors.joining(" ")));
    }
}
